﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using PowerHub.MES.Entities;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PowerHub.MES.EFBase
{
    /// <summary>
    /// 业务通用层EF封装
    /// </summary>
    public abstract class BaseServices<TEntity> where TEntity : BaseEntity
    {
        protected readonly BaseRepository<TEntity> _baseDAL;

        public BaseServices(PowerHubMesContext repositorySys)
        {
            _baseDAL = new BaseRepository<TEntity>(repositorySys);
        }
      
        /// <summary>
        /// 获取单条实体信息
        /// </summary>
        /// <param name="where"></param>
        /// <param name="query"></param>
        /// <param name="include"></param>
        /// <returns></returns>
        public virtual async Task<TEntity> GetEntityAsync(Expression<Func<TEntity, bool>> where)
        {
            return await _baseDAL.GetEntitys().Where(where).FirstOrDefaultAsync();
        }

        /// <summary>
        /// 获取集合
        /// </summary>
        /// <param name="where"></param>
        /// <param name="query"></param>
        /// <param name="include"></param>
        /// <returns></returns>
        public virtual async Task<List<TEntity>> GetEntityListAsync(Expression<Func<TEntity, bool>> where)
        {
            return await  _baseDAL.GetEntitys().Where(where).ToListAsync();
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="where"></param>
        /// <param name="query"></param>
        /// <param name="include"></param>
        /// <returns></returns>
        public virtual async Task<OperationPageResult<List<TEntity>>> GetPageListAsync(Expression<Func<TEntity, bool>> where, PageQuery query, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null)
        {
            return await _baseDAL.GetEFCoreBasePageAsync(where, query, include);
        }

    }
}
